﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;
using Sinacor.Infra.Service.Common;
using System.Data;

namespace Sinacor.OMM.Deployment.Configurator.ResourceAccess
{
    public class Receptor : BaseResourceAccess
    {

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public Receptor(Int32 companyId)
            : base(companyId)
        {

        }

        #endregion

        #region Methods

        public Entity.Receptor ConsultarReceptor()
        {
            //
            String sqlCommand = String.Empty;
            Entity.Receptor retorno = null;

            sqlCommand = @"SELECT a.COD_CANL_COMU
                                  ,a.NOME_CANL_COMU
                                  ,a.DESC_CANL_COMU
                                  ,a.NOME_USUA_CONX
                                  ,a.TXT_SENH_USUA_CONX
                                  ,a.NOME_FUNC_SESS
                                  ,a.COD_REPRIO_PRIN
                                  ,a.NOME_DRTO_ARQ_CTRL
                                  ,a.COD_REPRIO_MSG_SIST
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'CONNECTION_STRING' AND COD_REPRIO = a.COD_REPRIO_MSG_SIST) DESC_REPRIO_MSG_SIST
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'QUEUE_MESSAGE_LIMIT' AND COD_REPRIO = a.COD_REPRIO_MSG_SIST) MSG_LIMIT_REPRIO_MSG_SIST
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'QUEUE_BYTES_LIMIT' AND COD_REPRIO = a.COD_REPRIO_MSG_SIST) BYTES_LIMIT_REPRIO_MSG_SIST
                           FROM TBOCNFG_RCPT a";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                retorno = this.FindOne<Entity.Receptor>(command,
                    reader =>
                    {
                        Entity.Receptor item = new Entity.Receptor();

                        item.CodigoCanal = reader.GetValue<int>("COD_CANL_COMU");
                        item.NomeCanal = reader.GetValue<String>("NOME_CANL_COMU");
                        item.DescricaoCanal = reader.GetValue<String>("DESC_CANL_COMU");
                        item.UsuarioConexaoSTM = reader.GetValue<String>("NOME_USUA_CONX");

                        if(!string.IsNullOrEmpty(reader.GetValue<String>("TXT_SENH_USUA_CONX")))
                            item.SenhaConexaoSTM = PSE.Framework.Cryptography.RijndaelCryptographer.DecryptString(reader.GetValue<String>("TXT_SENH_USUA_CONX"));
                        else
                            item.SenhaConexaoSTM = reader.GetValue<String>("TXT_SENH_USUA_CONX");

                        item.FuncionarioConexaoSTM = reader.GetValue<String>("NOME_FUNC_SESS");
                        item.DiretorioArquivo = reader.GetValue<String>("NOME_DRTO_ARQ_CTRL");
                        item.Repositorio.Codigo = reader.GetValue<int>("COD_REPRIO_MSG_SIST");
                        item.Repositorio.Fila = reader.GetValue<String>("DESC_REPRIO_MSG_SIST");
                        item.Repositorio.LimiteMensagem = reader.GetValue<int>("MSG_LIMIT_REPRIO_MSG_SIST");
                        item.Repositorio.LimiteTamanho = reader.GetValue<int>("BYTES_LIMIT_REPRIO_MSG_SIST");

                        return item;
                    });
            }

            return retorno;
        }

        public void AlterarReceptor(Entity.Receptor entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"UPDATE TBOCNFG_RCPT SET
                                   NOME_CANL_COMU       = UPPER(:NOME_CANL_COMU)
                                  ,DESC_CANL_COMU       = UPPER(:DESC_CANL_COMU)
                                  ,NOME_USUA_CONX       = :NOME_USUA_CONX
                                  ,TXT_SENH_USUA_CONX   = :TXT_SENH_USUA_CONX
                                  ,NOME_FUNC_SESS       = :NOME_FUNC_SESS
                                  ,NOME_DRTO_ARQ_CTRL   = :NOME_DRTO_ARQ_CTRL
                           WHERE COD_CANL_COMU = :COD_CANL_COMU";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "COD_CANL_COMU", DbType.Int32, entity.CodigoCanal);
                this.DataBase.AddInParameter(command, "NOME_CANL_COMU", DbType.String, entity.NomeCanal);
                this.DataBase.AddInParameter(command, "DESC_CANL_COMU", DbType.String, entity.DescricaoCanal);
                this.DataBase.AddInParameter(command, "NOME_USUA_CONX", DbType.String, entity.UsuarioConexaoSTM);
                this.DataBase.AddInParameter(command, "TXT_SENH_USUA_CONX", DbType.String, entity.SenhaConexaoSTM);
                this.DataBase.AddInParameter(command, "NOME_FUNC_SESS", DbType.String, entity.FuncionarioConexaoSTM);
                this.DataBase.AddInParameter(command, "NOME_DRTO_ARQ_CTRL", DbType.String, entity.DiretorioArquivo);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        #endregion
    }
}
